﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Web.Security;
using System.Data;

public partial class Page_Query : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MembershipUser user = Membership.GetUser();
            int frameLevel = QuerySQL.GetUserFrameLevel((Guid)user.ProviderUserKey);
            if (frameLevel == -1)
            {
                if (user.UserName == "wangf0228")
                    HiddenField1.Value = "0";
                else
                {
                    FormsAuthentication.SignOut();
                    //Response.Redirect("~/Page/Default.aspx");
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('帐户存在异常，请联系管理员');window.location.href='Default.aspx'</script>");
                }
            }
            else
            {
                HiddenField1.Value = frameLevel.ToString();
            }
        }        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (RadioButton1.Checked)
        {
            Chart1.Visible = false;
            GridView1.Visible = true;
        }
        else
        {
            Chart1.Visible = true;
            GridView1.Visible = false;
        }
        if (RadioButton1.Checked)
        {
            BindDisplayList();
        }
        else
        {
            Chart1.Series[0].Points.Clear();
            Chart1.ChartAreas[0].AxisX.LabelStyle.Format = "{yyyy-MM-dd}";
            Chart1.Series[0].Points.DataBindXY(GetData().DefaultView, "时间", GetData().DefaultView, "温度");            
        }
        //UpdatePanel2.Update(); 
    }
    DataTable GetData()
    {
        int level = int.Parse(HiddenField1.Value);
        MembershipUser user = Membership.GetUser();
        switch (level)
        {
            case 0:
                break;
            case 1:
                if (!QuerySQL.CheckCallNumAuth_Frame1(TextBox1.Text, (Guid)user.ProviderUserKey))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('查询的电话号不存在，或者您没有相应的权限');window.location.href='Default.aspx'</script>");
                    return null;
                }
                break;
            case 2:
                if (!QuerySQL.CheckCallNumAuth_Frame2(TextBox1.Text, (Guid)user.ProviderUserKey))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('查询的电话号不存在，或者您没有相应的权限');window.location.href='Default.aspx'</script>");
                    return null;
                }
                break;
            case 3:
                if (!QuerySQL.CheckCallNumAuth_Frame3(TextBox1.Text, (Guid)user.ProviderUserKey))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('查询的电话号不存在，或者您没有相应的权限');window.location.href='Default.aspx'</script>");
                    return null;
                }
                break;
            case 4:
                if (!QuerySQL.CheckCallNumAuth_Frame4(TextBox1.Text, (Guid)user.ProviderUserKey))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('查询的电话号不存在，或者您没有相应的权限');window.location.href='Default.aspx'</script>");
                    return null;
                }
                break;
        }
        DataTable dt = QuerySQL.GetT_CallNum(TextBox1.Text, DateTime.Parse(TextBox2.Text), DateTime.Parse(TextBox3.Text));
        return dt;
    }
    private void BindDisplayList()
    {
        BindDisplayList(0);
    }
    private void BindDisplayList(int pageIndex)
    {
        DataTable tb = GetData();
        WFWebLib.GridViewOperation.BindPagingGridView(GridView1, tb.DefaultView, pageIndex);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        BindDisplayList(e.NewPageIndex);
        //UpdatePanel2.Update(); 
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
            WFWebLib.GridViewOperation.GridRowCreated(sender, e);
    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (RadioButton1.Checked)
        {
            Chart1.Visible = false;
            GridView1.Visible = true;
        }
        else
        {
            Chart1.Visible = true;
            GridView1.Visible = false;
        }
        Button1_Click(null, null);
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        if (RadioButton1.Checked)
        {
            Chart1.Visible = false;
            GridView1.Visible = true;
        }
        else
        {
            Chart1.Visible = true;
            GridView1.Visible = false;
        }
        Button1_Click(null, null);
    }
}